package com.anhry.app.safety.manage.oa.struts.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.oa.bean.TbOaMsgType;
import com.anhry.app.safety.manage.oa.struts.base.OABaseAction;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Page;

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class AcceptTypeAction extends OABaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List acceptList;
	private TbOaMsgType type;
	private String id;
	
	public String list(){
		try {
			int num = 1;		//初始页码
			int rowCount = 0;	//记录总数
			int size = 10;		//定义分页的每页显示的初始个数
			if(super.num != null && !"".equals(super.num)) {
				num = Integer.parseInt(super.num);
			}
			if(super.size != null && !"".equals(super.size)){
				size = Integer.parseInt(super.size);
			}
			StringBuffer hqlString = new StringBuffer("select count(*) from TbOaMsgType l where l.isDele=0");
			rowCount = this.utilService.getHqlQuerySize(hqlString.toString());
			if (rowCount > 0){
				Page page = new Page(size,num,rowCount);
				this.setPage(page);
				acceptList = (List<TbOaMsgType>)this.utilService.getHqlQuery(hqlString.toString().replace("select count(*) ", ""),page.getStartRow(),page.getSize());
			}
			this.setResult("msg_type_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("aqzs: LawAction:list() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	public String add(){
		try {
			type.setIsDele("0");
			this.utilService.save(type);
			tip = "添加成功！";
			list();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("AcceptTypeAction add()出错："+e);
		}
		return ERROR;
	}
	
	public String del(){
		try {
			if (!"".equals(id) && id != null){
				String hqlString = "from TbOaMsgType t where t.id = "+id;
				List<TbOaMsgType> msgTypeList = (List<TbOaMsgType>)this.utilService.getHqlQuery(hqlString);
				for(TbOaMsgType type:msgTypeList){
					type.setIsDele("1");
					this.utilService.update(type);
				}
				tip = "删除成功";
				list();
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("AcceptTypeAction del()出错："+e);
		}
		return ERROR;
	}
	
	public String edit(){
		try {
			if (type != null){
				TbOaMsgType msgType = (TbOaMsgType)this.utilService.getObjectById(TbOaMsgType.class, Long.parseLong(id));
				if (msgType != null){
					msgType.setTypeName(type.getTypeName());
					this.utilService.update(msgType);
					tip="修改成功！";
					list();
					return SUCCESS;
				} else {
					tip="修改失败！";
				}
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("AcceptTypeAction edit()出错："+e);
		}
		return ERROR;
	}
	
	public String load(){
		try {
			if (!"".equals(id) && id != null) {
				type = (TbOaMsgType) this.utilService.getObjectById(TbOaMsgType.class,Long.parseLong(id));
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("AcceptTypeAction edit()出错："+e);
		}
		return ERROR;
	}

	public List getAcceptList() {
		return acceptList;
	}

	public void setAcceptList(List acceptList) {
		this.acceptList = acceptList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public TbOaMsgType getType() {
		return type;
	}

	public void setType(TbOaMsgType type) {
		this.type = type;
	}
	
}
